{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6.8 RMSProp算法\n",
    "Adagrad算法优化了学习率的调整，但仍然存在一些问题。例如：它很多时候存在学习率过小的问题，收敛速度较慢。为此，科学家们又有针对性的提出了更多改进。RMSProp 算法就是其中比较典型的一种变体。本节咱们具体介绍它。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.8.1 基本思想\n",
    "RMSProp 算法是由 Geoffrey Hinton 提出的一种优化算法，主要用于解决梯度下降中的学习率调整问题。\n",
    "\n",
    "在梯度下降中，每个参数的学习率是固定的。但在实际应用中，每个参数的最优学习率可能是不同的。如果学习率过大，则模型可能会跳出最优值；如果学习率过小，则模型的收敛速度可能会变慢。\n",
    "\n",
    "RMSProp 算法通过自动调整每个参数的学习率来解决这个问题。它在每次迭代中维护一个指数加权平均值，用于调整每个参数的学习率。如果某个参数的梯度较大，则RMSProp算法会自动减小它的学习率；如果梯度较小，则会增加学习率。这样可以使得模型的收敛速度更快。\n",
    "\n",
    "这种算法的计算公式如下：\n",
    "\n",
    "$$g_t \\leftarrow \\rho g_{t-1} + (1-\\rho) \\frac{\\partial L}{\\partial w}^2$$\n",
    "\n",
    "$$w \\leftarrow w - \\frac{\\eta}{\\sqrt{g_t + \\epsilon}} \\frac{\\partial L}{\\partial w}$$\n",
    "\n",
    "其中 $g_t$ 是指数加权平均值，$\\rho$ 是衰减率，$\\eta$ 是学习率，$\\epsilon$ 是为了防止分母为0而添加的一个很小的常数。在每次迭代时，RMSProp 算法会计算参数的梯度平方值的指数加权平均值 $g_t$，然后使用这个值来调整参数的学习率。这样可以使得模型的收敛速度更快，并且能够自动调整学习率。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.8.2 算法优缺点\n",
    "\n",
    "任何事物都是有两面性的，深度学习的每种算法都不例外。明确它们的优缺点才能更好的理解它们。对RMSProp算法而言，尽管它在Adagrad算法的基础上进行了改进，但依然优缺点都很突出。\n",
    "\n",
    "优点方面，RMSProp算法能够自动调整学习率，使得模型的收敛速度更快。它可以避免学习率过大或过小的问题，能够更好地解决学习率调整问题。实现上看它较为简单，适用于各种优化问题。\n",
    "\n",
    "缺点方面，它在处理稀疏特征时可能不够优秀。此外，它需要调整超参数，如衰减率 $\\rho$ 和学习率 $\\eta$，这需要一定的经验。还有，收敛速度可能不如其他我们后面会介绍的优化算法，例如 Adam算法。\n",
    "\n",
    "不过，瑕不掩瑜。RMSProp算法还是一种优化算法发展进程中非常优秀的算法。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.8.3 代码示例\n",
    "\n",
    "我们用一个简单的线性回归的例子来演示RMSProp算法的pytorch代码实现，方便你的理解。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAEGCAYAAABiq/5QAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/d3fzzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAjL0lEQVR4nO3dd3xc1Z338c9PzbJkFavYKpYs23JB7rbccSgOoKVDlix1WULCQggQkg0hm33CbnY32X3CbkggJDjBQEhoAVNiCM2ATTE2cu+9yVa1LUtuquf5Y8bEj7GMsDVzR3O/79drXpo5Gun+zkv2994599xzzTmHiIj4R4zXBYiISHgp+EVEfEbBLyLiMwp+ERGfUfCLiPhMnNcFdEZWVpYrKiryugwRkW5l8eLFdc657OPbu0XwFxUVUV5e7nUZIiLdipltP1G7hnpERHxGwS8i4jMKfhERn1Hwi4j4jIJfRMRnFPwiIj6j4BcR8ZmoDv5319Xw6/c2e12GiEhEierg/3BTHQ+8vYG2dt1zQETkqKgO/mG5qTS1trNtz0GvSxERiRjRHfw5KQCsq2z0uBIRkcgR1cFf3KcXsTHGuqoGr0sREYkYU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gUWd/q3Ouah9ABcCG4DNwA+9ridEfTyTwEe8FcCy4ONCIJPALICNwa8ZXtcaov6fDcwJPo/6PgNjgPLg3/sloHe09xv4N2AdsAp4EugRjX0GniZwHqOFwBH9zSfrJ/DDYLatB/7mi2xLSzaIiPhMNA/1iIjICSj4RUR8RsEvIuIzCn4REZ9R8IuI+IyCXwQwszYzW3bMo8uuiDWzomNXXBTxWpzXBYhEiMPOuTFeFyESDjriFzkJM9tmZv9tZouCj+Jge38zm2tmK4JfC4Ptfc3sRTNbHnxMDf6qWDP7bXBd+TfNrKdnnRLfU/CLBPQ8bqjn7475XoNzbiLwEIFVQQk+/71zbhTwR+CXwfZfAvOcc6MJrKOzOtg+GPiVc244UA98JaS9ETkJXbkrApjZAedcrxO0bwPOdc5tCS6GV+WcyzSzOiDXOdcSbK90zmWZWS3QzznXdMzvKALecoGbaWBm3wfinXP/EYauiXyGjvhFPp/r4HlH7zmRpmOet6Hza+IhBb/I5/u7Y74uCD7/iMDKoADXAR8En88FboNP7wmcGq4iRTpLRx0iAT3NbNkxr193zh2d0tnDzBYSOFC6Jth2JzDLzL5H4K5YNwXb7wJmmtnNBI7sbyOw4qJIxNAYv8hJBMf4S51zdV7XItJVNNQjIuIzOuIXEfEZHfGLiPiMgl9ExGcU/CIiPqPgFxHxGQW/iIjP/D8HazBmBbDIGgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import os\n",
    "os.environ[\"KMP_DUPLICATE_LIB_OK\"] = \"TRUE\"\n",
    "import torch\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 假设我们有一个简单的线性回归模型\n",
    "# y = w * x + b\n",
    "# 其中 w 和 b 是需要学习的参数\n",
    "\n",
    "# 定义超参数\n",
    "learning_rate = 0.01\n",
    "num_epochs = 100\n",
    "\n",
    "# 随机生成训练数据\n",
    "X = torch.randn(100, 1)\n",
    "y = 2 * X + 3 + torch.randn(100, 1)\n",
    "\n",
    "# 初始化参数\n",
    "w = torch.zeros(1, requires_grad=True)\n",
    "b = torch.zeros(1, requires_grad=True)\n",
    "\n",
    "# 创建 RMSProp optimizer\n",
    "optimizer = torch.optim.RMSprop([w, b], lr=learning_rate)\n",
    "\n",
    "# 记录每次迭代的 loss\n",
    "losses = []\n",
    "\n",
    "# 训练模型\n",
    "for epoch in range(num_epochs):\n",
    "  # 计算预测值\n",
    "  y_pred = w * X + b\n",
    "\n",
    "  # 计算 loss\n",
    "  loss = torch.mean((y_pred - y) ** 2)\n",
    "\n",
    "  # 记录 loss\n",
    "  losses.append(loss.item())\n",
    "\n",
    "  # 清空上一步的梯度\n",
    "  optimizer.zero_grad()\n",
    "\n",
    "  # 计算梯度\n",
    "  loss.backward()\n",
    "\n",
    "  # 更新参数\n",
    "  optimizer.step()\n",
    "\n",
    "# 可视化训练过程\n",
    "plt.plot(losses)\n",
    "plt.xlabel('Epoch')\n",
    "plt.ylabel('Loss')\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "RMSProp 算法在训练过程中是通过自动调整学习率来优化模型参数的。因此，它并没有明显的搜索过程。但是，我们可以通过观察训练过程中的损失，了解模型的训练情况。如果损失在不断降低，则说明模型的训练效果较好；如果损失不再降低，则说明模型可能已经达到了最优解或者出现了过拟合。\n",
    "这个过程中我们可以判断模型的训练效果，并适当调整超参数，以提高模型的泛化能力。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**梗直哥提示：如果简单了解RMSProp算法的基本原理并不难，但这样其实只掌握了其功力的30%。最精华的其实是为什么提出这种算法，人家怎么就能提出这种算法，当时是怎么想的等等一系列更加深入的问题。只有这样才能不光知其然，还能知其所以然，充分提升自己深度学习的境界。欢迎来到哥的课堂，就这些进阶问题，帮你武装自己。更多了解加V：gengzhige99**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[Next 6-9 Adadelta算法](./6-9%20Adadelta算法.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
